package com.zlll.winner.mapper.bbs;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.business.model.bbs.BbsUserComment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zlll.winner.business.vo.bbs.BbsSubjectVo;
import com.zlll.winner.business.vo.bbs.BbsUserCommentVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 * 论坛用户评论表 Mapper 接口
 * </p>
 *
 * @author Walert
 * @since 2019-09-12
 */
public interface BbsUserCommentMapper extends BaseMapper<BbsUserComment> {

    @Select("select " +
            "(select if(count(tt.id)>0,1,0) from bbs_user_action_log tt where tt.user_id = ${userId} and tt.biz_id = t.id and tt.biz_type = 2 and tt.action_type = 1 and tt.is_del = 0) as is_like, "+
            "m1.user_name as replyUserName " +
            ",m1.content as replyContent " +
            ",m2.phone as userPhone " +
            ",GROUP_CONCAT(distinct m4.name) as  userType " +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds " +
            ",t.* " +
            "from bbs_user_comment t " +
            "left join bbs_user_comment m1 on (m1.id = t.parent_id and m1.is_del = 0) " +
            "left join lht_user m2 on (t.user_id = m2.id and m2.is_del = 0) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "${ew.customSqlSegment} " +
            "group by t.id " +
            "order by t.create_time desc ")
    IPage<BbsUserCommentVo> selectPageByCustomer(IPage<BbsUserCommentVo> page, @Param("ew") Wrapper<BbsUserCommentVo> queryWrapper,@Param("userId") Long userId);

    @Select("select " +
            "(select if(count(tt.id)>0,1,0) from bbs_user_action_log tt where tt.user_id = ${userId} and tt.biz_id = t.id and tt.biz_type = 2 and tt.action_type = 1 and tt.is_del = 0) as is_like, "+
            "m1.user_name as replyUserName " +
            ",m1.content as replyContent " +
            ",m2.phone as userPhone " +
            ",GROUP_CONCAT(distinct m4.name) as  userType " +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds " +
            ",t.* " +
            "from bbs_user_comment t " +
            "left join bbs_user_comment m1 on (m1.id = t.parent_id and m1.is_del = 0) " +
            "left join lht_user m2 on (t.user_id = m2.id and m2.is_del = 0) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "${ew.customSqlSegment} " +
            "group by t.id " +
            "order by m4.sort desc,t.create_time desc ")
    IPage<BbsUserCommentVo> selectPageByCustomerGuard(IPage<BbsUserCommentVo> page, @Param("ew") Wrapper<BbsUserCommentVo> queryWrapper,@Param("userId") Long userId);


    @Select("select " +
            "(select if(count(tt.id)>0,1,0) from bbs_user_action_log tt where tt.user_id = ${userId} and tt.biz_id = t.id and tt.biz_type = 2 and tt.action_type = 1 and tt.is_del = 0) as is_like, "+
            "m1.user_name as replyUserName " +
            ",m1.content as replyContent " +
            ",m2.phone as userPhone " +
            ",GROUP_CONCAT(distinct m4.name) as  userType " +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds " +
            ",t.* " +
            "from bbs_user_comment t " +
            "left join bbs_user_comment m1 on (m1.id = t.parent_id and m1.is_del = 0) " +
            "left join lht_user m2 on (t.user_id = m2.id and m2.is_del = 0) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "where t.id = ${commentId} and t.is_del = 0 " +
            "group by t.id " +
            "order by t.create_time desc ")
    BbsUserCommentVo selectByCommentId(@Param("commentId")Long commentId,@Param("userId") Long userId);

    @Select("select " +
            "(select if(count(tt.id)>0,1,0) from bbs_user_action_log tt where tt.user_id = ${userId} and tt.biz_id = t.id and tt.biz_type = 2 and tt.action_type = 1 and tt.is_del = 0) as is_like, "+
            "m1.user_name as replyUserName " +
            ",m1.content as replyContent " +
            ",m2.phone as userPhone " +
            ",GROUP_CONCAT(distinct m4.name) as  userType " +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds " +
            ",t.* " +
            "from bbs_user_comment t " +
            "left join bbs_user_comment m1 on (m1.id = t.parent_id and m1.is_del = 0) " +
            "left join lht_user m2 on (t.user_id = m2.id and m2.is_del = 0) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "${ew.customSqlSegment} " +
            "group by t.id " +
            "order by t.create_time desc ")
    List<BbsUserCommentVo> getDetailsList(@Param("ew") Wrapper<BbsUserCommentVo> queryWrapper, @Param("userId") Long userId);

}
